package com.sunday.framework.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sunday.common.core.asserts.BusinessAssert;
import com.sunday.common.core.converter.MapperConvert;
import com.sunday.common.core.enums.ErrorCodeEnum;
import com.sunday.common.core.remoting.base.page.PageResultDTO;
import com.sunday.common.mybatis.plus.query.PageWrapper;
import com.sunday.framework.dao.RoleDao;
import com.sunday.framework.dto.*;
import com.sunday.framework.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 角色表 服务实现类
 * @author: zsy
 * @Create: 2022/11/07 16:59:47
 */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * @Description: 新增 角色表 数据
     * @Param: [saveRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2022/11/07 16:59:47
     */
    @Override
    public void save(SaveRoleReqDTO saveRoleReqDTO) {
        Role role = MapperConvert.getFacade().map(saveRoleReqDTO, Role.class);
        role.setCreateTime(LocalDateTime.now());
        BusinessAssert.isTrue(this.roleDao.save(role), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 删除 角色表 数据
     * @Param: [removeRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2022/11/07 16:59:47
     */
    @Override
    public void remove(RemoveRoleReqDTO removeRoleReqDTO) {
        BusinessAssert.isTrue(this.roleDao.removeByIds(removeRoleReqDTO.getIds()), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 修改 角色表 数据
     * @Param: [updateRoleReqDTO]
     * @Return: void
     * @Author: zsy
     * @Time: 2022/11/07 16:59:47
     */
    @Override
    public void update(UpdateRoleReqDTO updateRoleReqDTO) {
        LambdaUpdateWrapper<Role> roleUW = Wrappers.lambdaUpdate();
        roleUW.eq(Role::getId, updateRoleReqDTO.getId());
        roleUW.set(Role::getName, updateRoleReqDTO.getName());
        BusinessAssert.isTrue(this.roleDao.update(roleUW), ErrorCodeEnum.A0_440);
    }

    /**
     * @Description: 获取 角色表 分页列表数据
     * @Param: [pageRoleReqDTO]
     * @Return: com.sunday.common.core.remoting.base.page.PageResultDTO<com.sunday.user.center.pojo.dto.role.PageRoleResDTO>
     * @Author: zsy
     * @Time: 2022/11/07 16:59:47
     */
    @Override
    public PageResultDTO<PageRoleResDTO> page(PageRoleReqDTO pageRoleReqDTO) {
        LambdaQueryWrapper<Role> roleQW = Wrappers.lambdaQuery();
        roleQW.eq(ObjectUtils.isNotEmpty(pageRoleReqDTO.getId()), Role::getId, pageRoleReqDTO.getId())
                .eq(StringUtils.isNotBlank(pageRoleReqDTO.getName()), Role::getName, pageRoleReqDTO.getName())
                .eq(ObjectUtils.isNotEmpty(pageRoleReqDTO.getCreateTime()), Role::getCreateTime, pageRoleReqDTO.getCreateTime());

        IPage<Role> page = this.roleDao.page(PageWrapper.map(pageRoleReqDTO, Role::getId), roleQW);
        List<PageRoleResDTO> records = Optional.ofNullable(page.getRecords())
                .map(data ->
                        data.stream().map(o ->
                                MapperConvert.getFacade().map(o, PageRoleResDTO.class)).collect(Collectors.toList())
                )
                .orElse(Collections.emptyList());
        return PageWrapper.result(page, records);
    }

}
